package com.sz.demo.discount;

import com.sz.demo.model.Fruit;
import com.sz.demo.model.Product;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 百分比折扣
 * @author Lenovo
 */
public class PercentageDiscount implements DiscountStrategy {
    private BigDecimal discountRate;

    public PercentageDiscount(BigDecimal discountRate) {
        this.discountRate = discountRate;
    }

    @Override
    public BigDecimal applyDiscount(Product product, int quantity) {
        // 结合策略折扣和商品自身的折扣
        BigDecimal discount = discountRate.multiply(product.getDiscountRate());
        return product.getBasePrice()
                .multiply(discount)
                .setScale(2, RoundingMode.HALF_UP);
    }
}